home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / dsp / a56_10sh.z / a56_10sh / subs.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  3KB  |  167 lines

  1. /*******************************************************
  2.  *
  3.  *  a56 - a DSP56001 assembler
  4.  *
  5.  *  Written by Quinn C. Jensen
  6.  *  July 1990
  7.  *  jensenq@npd.novell.com (or jensenq@qcj.icon.com)
  8.  *
  9.  *******************************************************\
  10.  
  11. /*
  12.  * Copyright (C) 1990, 1991 Quinn C. Jensen
  13.  *
  14.  * Permission to use, copy, modify, distribute, and sell this software
  15.  * and its documentation for any purpose is hereby granted without fee,
  16.  * provided that the above copyright notice appear in all copies and
  17.  * that both that copyright notice and this permission notice appear
  18.  * in supporting documentation.  The author makes no representations
  19.  * about the suitability of this software for any purpose.  It is
  20.  * provided "as is" without express or implied warranty.
  21.  *
  22.  */
  23. static char *Copyright = "Copyright (C) 1990, 1991 Quinn C. Jensen";
  24.  
  25. /*
  26.  *  subs.c - Some subroutines for the assembler.
  27.  *
  28.  */
  29.  
  30. #include "a56.h"
  31.  
  32. #define MAX 1024
  33.  
  34. char *alloc();
  35.  
  36. FILE *open_read(file)
  37. char *file;
  38. {
  39.     FILE *fp;
  40.  
  41.     if(strcmp(file, "-") == 0)
  42.     fp = stdin;
  43.     else if ((fp = fopen(file, "r")) == NULL) {
  44.         perror(file);
  45.         exit(1);
  46.     }    
  47.     return(fp);
  48. }
  49.  
  50. FILE *open_write(file)
  51. char *file;
  52. {
  53.     FILE *fp;
  54.     if ((fp = fopen(file, "w")) == NULL) {
  55.         perror(file);
  56.         exit(1);
  57.     }    
  58.     return(fp);
  59. }
  60.  
  61. FILE *open_append(file)
  62. char *file;
  63. {
  64.     FILE *fp;
  65.     if ((fp = fopen(file, "a")) == NULL) {
  66.         perror(file);
  67.         exit(1);
  68.     }    
  69.     return(fp);
  70. }
  71.  
  72. fatal(c, a1, a2, a3, a4, a5, a6, a7, a8)
  73. char *c, *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
  74. {
  75.     fprintf(stderr, c, a1, a2, a3, a4, a5, a6, a7, a8);
  76.     exit(1);
  77. }
  78.  
  79. #define TABS 8
  80. #define MAX_BUF 256
  81.  
  82. char tabbuf[MAX_BUF], *untabn();
  83. char *untab(s)    /* expand tabs in s */
  84. register char *s;
  85. {
  86.     return(untabn(s, TABS));
  87. }
  88.  
  89. char *untabn(s, stops)    /* expand tabs in s */
  90. register char *s;
  91. register int stops;
  92. {
  93.     char *o = s;
  94.  
  95.     /* copy input string into buffer to scan while input string is modified */
  96.  
  97.     register char *b = tabbuf;
  98.     
  99.     strncpy(b, s, MAX_BUF);
  100.  
  101.     /* iterate until the copy of the input string is depleted */
  102.  
  103.     while(*b) {
  104.         if(*b == '\t') {
  105.         do
  106.                 *s = ' ';
  107.         while ((++s - o) % stops);
  108.         } else {
  109.             *s = *b; s++;
  110.         }
  111.         b++;
  112.     }
  113.  
  114.     /* null terminate the resultant string */
  115.  
  116.     *s = '\0';
  117.  
  118.     return(o);
  119. }
  120.  
  121. char *alloc(size)
  122. int size;
  123. {
  124.     char *p = (char *)malloc(size);
  125.     if(NOT p)
  126.     fatal("alloc:  insufficient virtual memory to allocate %d bytes\n", 
  127.         size);
  128.     return(p);
  129. }
  130.  
  131. #define ascii2n(c)  \
  132.     ((c) >= 'a' ? (c) - 'a' + 10 : ((c) >= 'A' ? (c) - 'A' + 10 : (c) - '0'))
  133.  
  134. #define valid(c) ((c) >= '0' && (c) <= '9' || \
  135.     (c) >= 'A' && (c) <= 'Z' || \
  136.     (c) >= 'a' && (c) <= 'z')
  137.  
  138. strtol(s, p, base)
  139. register char *s, **p;
  140. register int base;
  141. {
  142.     register long result = 0;
  143.     register int sign = 0;
  144.  
  145.     while(*s == ' ' || *s == '\t')
  146.     s++;
  147.  
  148.     if(*s == '-') {
  149.     s++;
  150.     sign++;
  151.     }
  152.  
  153.     while(valid(*s)) {
  154.     register int dig = ascii2n(*s);
  155.     if(dig >= base)
  156.         break;
  157.     result *= base;
  158.     result += dig;
  159.      s++;
  160.     }
  161.  
  162.     if(p)
  163.     *p = s;
  164.  
  165.     return (sign ? -result : result);
  166. }
  167.